package solution.tree;

import common.TreeNode;
import common.util.TreeNodeUtil;

/**
 * @author : wenchao.long
 * @date : Created in 2021/07/08 17:09 周四
 * @description : 二叉树的最小深度
 */
public class MinDepthSolution {

    public int minDepth(TreeNode root) {
        return dfs(root);
    }

    private int dfs(TreeNode node) {
        if (node == null) {
            return 0;
        }
        if (node.left == null && node.right == null) {
            return 1;
        }
        int depth = Integer.MAX_VALUE;
        if (node.left != null) {
            depth = Math.min(dfs(node.left), depth);
        }
        if (node.right != null) {
            depth = Math.min(dfs(node.right), depth);
        }
        return depth + 1;
    }

    public static void main(String[] args) {
        TreeNode treeNode = TreeNodeUtil.create(3, 9, 20, null, null, 15, 7);
        System.out.println(new MinDepthSolution().minDepth(treeNode));
    }


}
